var app = getApp();
Page({
    data: {
        index: 0,

        addressData:{},         // 地址数据
        
        trackingNum:'',         // 物流单号
        remark:'',              // 备注
        id: "",                 // 详情id
        trackingInfo:[],        // 物流公司数据
    },
    bindPickerChange(e) {
        this.setData({
            index: e.detail.value
        })
    },
    // 输入赋值
    inputEvent(e){
        var value = e.detail.value;
        var type = e.target.dataset.type;

        if(type == 'order_number'){
            this.setData({
                trackingNum : value
            })
        }
        if (type == 'remark'){
            this.setData({
                remark : value
            })
        }
    },
    // 订单验证
    verifyForm() {
        var index =  this.data.index;
        var data = {};
        data.tracking_number    =  this.data.trackingNum;
        data.tracking_id        =  this.data.trackingInfo[index].id;
        data.tracking_company   =  this.data.trackingInfo[index].name;
        data.remark             =  this.data.remark;
        data.id                 =  this.data.id;

        if (!data.tracking_number) {
            wx.showToast({ image: '../../img/warning.png', title: '请输入物流单号' });
            return false;
        }
        if (!data.tracking_id || !data.tracking_company ) {
            wx.showToast({ image: '../../img/warning.png', title: '请选择物流公司' });
            return false;
        }
        if (!data.remark) {
            wx.showToast({ image: '../../img/warning.png', title: '请输入备注' });
            return false;
        }

        this.submitOrder(data);
    },
    // 提交订单
    submitOrder(data) {
        var self = this;
        var url = "/entrust/order_save";

        wx.getStorage({
            key: 'user',
            success: function (res) {
                data.openid = res.data.openid;
                data.token = res.data.token;
                wx.request({
                    url: app.url.requestUrl + url,
                    method: 'POST',
                    data: data,
                    success: function (datas) {
                        var list = datas.data;
                        if (list.code == 1) {
                            wx.showLoading({title: '操作成功'})
                            setTimeout(function(){
                                wx.hideLoading();
                                wx.redirectTo({
                                    url: '../trading-entrust/trading-entrust'
                                })
                            },500)
                        }else{
                            wx.showToast({
                                image:'../../img/warning.png',
                                title: list.msg ,
                            })
                        }
                    }
                })
            },
        })
    },
    checkboxChange(e) {
        console.log('checkbox发生change事件，携带value值为：', e.detail.value)
    },
    onLoad(options){
        if (!options.id){
            wx.showLoading({
                title: '数据错误',
            })
            wx.navigateBack({
                delta: 1
            })
            return false;
        }
        this.setData({
            id : parseInt(options.id)
        })
        this.getAddress();
        this.getLogistics();
    },
    // 获取地址
    getAddress(){
        var self = this;
        var url = "/entrust/get_adr";
        var id = this.data.id;

        wx.getStorage({
            key: 'user',
            success: function (res) {
                wx.request({
                    url: app.url.requestUrl + url,
                    method: 'POST',
                    data: {
                        openid : res.data.openid,
                        token : res.data.token,
                        id
                    },
                    success: function (data) {
                        var list = data.data;
                        if (list.code == 1) {
                            self.setData({
                                addressData: list.data
                            })
                        }
                    }
                })
            },
        })
    },
    getLogistics(){
        var self = this;
        var url = "/entrust/company";

        wx.getStorage({
            key: 'user',
            success: function (res) {
                wx.request({
                    url: app.url.requestUrl + url,
                    method: 'POST',
                    data: {
                        openid: res.data.openid,
                        token: res.data.token
                    },
                    success: function (data) {
                        var list = data.data;
                        if (list.code == 1) {
                            self.setData({
                                trackingInfo: list.data
                            })
                        }
                    }
                })
            },
        })
    }
})